package com.lnb.controller;


import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lnb.entity.Roles;
import com.lnb.entity.Users;
import com.lnb.entity.UsersParam;
import com.lnb.service.IRolesService;
import com.lnb.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liningbo
 * @since 2020-05-21
 */
@RestController
@RequestMapping("/roles")
public class RolesController {

	@Autowired
	IRolesService service;
	
	/**
	 * 多条件分页查询
	 * @return
	 */
	@GetMapping("/getList")
	public Object getList(@RequestParam(required = false,defaultValue = "1")Integer page,
			@RequestParam(required = false,defaultValue = "10")	Integer limit) {
		//分页-需要分页的配置文件
		Page<Roles> pageObj = new Page<>(page,limit);		
		IPage<Roles> ipage = service.page(pageObj, null);	
		List<Roles> data = ipage.getRecords();
		long total = ipage.getTotal();
		return new Result(0,"",data,total);
	}
	
	@PostMapping("/insert")
	public Object insert(@Valid Roles entity) {
		boolean res = service.save(entity);
		if(res) {
			return new Result(0,"添加成功");
		}else {
			return new Result(1,"添加失败");
		}
	}
	@PostMapping("/update")
	public Object update(@Valid Roles entity,@RequestParam Integer id) {
		entity.setId(id);
		boolean res = service.updateById(entity);
		if(res) {
			return new Result(0,"修改成功");
		}else {
			return new Result(1,"修改失败");
		}
	}
	
	
	
	
	@GetMapping("/delete")
	public Object delete(@RequestParam Integer id) {
		boolean res = service.removeById(id);
		if(res) {
			return new Result(0,"删除成功");
		}else {
			return new Result(1,"删除失败");
		}
	}
		
		@GetMapping("/getAllList")
		public Object getAllList(){
			return service.list();
			}
			
	

}
